/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------

*/
/*
 * Role.java
 *
 * Created on January 17, 2003, 9:45 AM
 */

package com.webdeninteractive.bie.persistent.privileges;

import java.sql.*;
import java.util.*;
import com.webdeninteractive.bie.persistent.*;
import java.io.*;

/**
 *
 * @author  jd
 */
public class Role extends Persistent{
    
    private String name;
    private int oid;
    private String description = "";
    private ArrayList users = new ArrayList();
    private ArrayList resources = new ArrayList();
        
    /** Creates a new instance of Role */
    private Role(int oid, String name) throws SQLException,IOException{
        this.oid = oid;
        this.name = name;

        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
                                
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            rs = s.executeQuery("select * from User_Role where roleOID= " + oid);                                    
            while (rs.next()) {
                users.add(new Integer(rs.getInt("userOID")));
            }
            
            rs = s.executeQuery("select * from Role_Resource where roleOID= " + oid);            
            while (rs.next()) {
                resources.add(new Integer(rs.getInt("resourceOID")));
            }                        
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }            
         
    }
 
    public String getName() {
        return this.name;    
    }
    
    public int getOID() {
        return this.oid;
    }

    public String getDescription() {
        return this.description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
        
    public void addUser(User user) throws SQLException,IOException{
        int incrementer = Controller.getInstance().generateOID("User_Role");         
        
        Connection conn = null;
        Statement s = null;
        try {
              
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();            
            
            //insert to database
            String sql = "insert into User_Role (OID,roleOID,roleName,userOID,userName) ";
            sql += "values (" + incrementer + "," + this.oid + ",'" + this.name + "'," + user.getOID() + ",'" + user.getName() + "')";
                       
            s.execute(sql);                                                
            conn.commit();            
            users.add(new Integer(user.getOID()));
        
        }finally {
            try {
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }    
    }
    
    public void removeUser(User user) throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        try {                                    
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            
            String sql = "delete from User_Role where userOID=" + user.getOID() + " and roleOID = " + this.oid;                       
            s.execute(sql);                                                
            conn.commit();            
            users.remove(new Integer(user.getOID()));            
        }finally {
            try {
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }    
                                 
    }

    public void addResource(Resource resource) throws SQLException,IOException{
        int incrementer = Controller.getInstance().generateOID("Role_Resource");         
        
        Connection conn = null;
        Statement s = null;
        try {
              
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();            
            
            //insert to database
            String sql = "insert into Role_Resource (OID,roleOID,resourceOID) ";
            sql += "values (" + incrementer + "," + this.oid + "," + resource.getOID() + ")";
                       
            s.execute(sql);                                                
            conn.commit();            
            resources.add(new Integer(resource.getOID()));
        
        }finally {
            try {
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }    
    }
    
    public void removeResource(Resource resource) throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        try {                                    
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            
            String sql = "delete from Role_Resource where resourceOID=" + resource.getOID() + " and roleOID = " + this.oid;                       
            s.execute(sql);                                                
            conn.commit();            
            resources.remove(new Integer(resource.getOID()));            
        }finally {
            try {
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }    
                                 
    }
        
    public boolean isUserIncluded(User user) {
        return users.contains(new Integer(user.getOID()));
    }
           
    public boolean isResourceIncluded(Resource resource) {
        return resources.contains(new Integer(resource.getOID()));        
    }
    
    public ArrayList getResources() throws SQLException,IOException{
        ArrayList resc = new ArrayList();
        for (int i=0;i<resources.size();i++) {
            Integer id = (Integer)resources.get(i);                        
            resc.add(Resource.findByOID(id.intValue()));
        }
        return resc;
    }
    
    public ArrayList getUsers() throws SQLException,IOException{
        ArrayList u = new ArrayList();
        for (int i=0;i<users.size();i++) {
            Integer id = (Integer)users.get(i);                        
            u.add(User.findByOID(id.intValue()));
        }
        return u;              
    }
    
    public static ArrayList findAll() throws SQLException,IOException{    
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            ArrayList al = null;
            al = new ArrayList();
            rs = s.executeQuery("select * from Roles");
            
            while (rs.next()) {
                al.add(load(rs));
            }
            return al;
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }    
    }
        
    public static Role findByOID(int OID) throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            rs = s.executeQuery("select * from Roles where OID = " + OID);
            rs.next();
            return load(rs);
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
    
    
    }
    
    public static Role findByName(String name) throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            
            rs = s.executeQuery("select * from Roles where name = '" + name + "'");
            rs.next();
            return load(rs);
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }        
    
    }
    
    public static Role create (String name) throws SQLException,IOException{        
        int roleIncrementer =  Controller.getInstance().generateOID("Roles");
        int userRoleIncrementer = Controller.getInstance().generateOID("User_Role");         
        
        Connection conn = null;
        Statement s = null;
        
        try {
            
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            
            String sql = "insert into Roles (OID,name,description) values (" + roleIncrementer + ",'" + name + "','')";                                    
            s.execute(sql);

            sql = "insert into User_Role (OID,roleOID,roleName,userOID,userName) ";
            sql += "values (" + userRoleIncrementer + "," + roleIncrementer + ",'" + name + "',1,'admin')";
                       
            s.execute(sql);                                                                        
            conn.commit();
        }finally {
            try {
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
        
        Role role = new Role(roleIncrementer,name);
        return role;                        
    }
    
    public void remove() throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        
        if (this.oid == 1) {
            throw new SQLException("The administrator role can not be removed");
        }
        
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            String sql ="delete from Roles where OID=" + this.oid;;
            s.execute(sql);
            
            sql ="delete from Role_Resource where roleOID=" + this.oid;;
            s.execute(sql);

            sql ="delete from User_Role where roleOID=" + this.oid;;
            s.execute(sql);
                        
            conn.commit();            
        }finally {
            try {
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }        
    }
            
    public void commitObject() throws SQLException,IOException{
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;
        
        try {
            conn = DataSourceFactory.getConnection();
            s = conn.createStatement();
            String sql ="update Roles set description='" + this.description + "' where OID=" + this.oid;
            s.execute(sql);
            conn.commit();
            
        }finally {
            try {
                if (rs != null) rs.close();
                if (s != null) s.close();
                if (conn != null) conn.close();
            }catch (Exception e2) {}
        }
        
    }

    private static Role load(ResultSet rs) throws SQLException,IOException{
        Role role = new Role(rs.getInt("OID"),rs.getString("name"));
        role.setDescription(rs.getString("description"));
        return role;
    }
    
    
    
}
